<?php




/**
* Edit an entity in [ images ]
*/

$images = new images();

# Handle Editing, when data is supplied
if($variable->post('edit-action', 'string', '') && ($image_id = $variable->post('image_id', 'integer', 0)))
{
	# Editing....
	$code = $variable->post('protection_code', 'string', '');
	$data = $variable->post('images', 'array', array());
	
	# Mark when this data was modified last time.
	$data['modified_on'] = 'UNIX_TIMESTAMP(CURRENT_TIMESTAMP())';
	$data['modified_counter'] = 'modified_counter+1';
	
	if($success = $images->edit(
		$data, # Posted data
		array(
			'image_id' => $image_id,
		),
		$code, # Security code related to this entry
		$image_id
	))
	{
		/**
		* Edit the tags/keywords
		*/
		$tags = new tags();
		$tags->correct_image_tags($image_id, $data['image_keywords']);
		#print_r($tags);
		#print_r($_POST); die();
		# [image_keywords]
		
		$messenger = new messenger('success', 'The record has been modified.');
		
		stopper::url(url::last_page('images-list.php?context=permissions'));
	}
	else
	{
		stopper::url('images-edit-error.php');
	}
}
else
{
	/**
	* Otherwise, load the details of the entity before editing it.
	*/
	if($image_id = $variable->get('id', 'integer', 0))
	{
		$details = $images->details($image_id);
		if(!$details)
		{
			# Data about this entity was not available
			stopper::url('images-edit-error.php?context=data');
		}
		# Purpose of this code block is to make sure that the variable
		# gets all indices with blank data, to feed to EDIT form.
		$details = $images->validate('edit', $details);

		/**
		* Build Smarty Variable with FULL details
		*/
		$smarty->assign('images', $details);
	}
	else
	{
		# Really Bad...
		stopper::url('images-direct-access-error.php');
	}
}
?>